package com.admission.web;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Academy;
import com.admission.model.Major;
import com.admission.service.AcademyManager;
import com.admission.service.MajorManager;
import com.admission.util.FileUtils;
import com.opensymphony.xwork2.ActionContext;
@Namespace("/base")
@Results({@Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class AcademyAction extends CrudActionSupport<Academy>{

	private static final long serialVersionUID = 488185923443490311L;
	private String id;
	//private Page<Academy> page = new Page<Academy>(PageUtils.DEFAULT_PAGE_SIZE);
	
	private Academy entity;
	private AcademyManager academyManager;
	private MajorManager majorManager;
	private List<Academy> academyList; //返回所有的学院
	private List<Major> majorList;
	

	public Academy getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p=new PropertyFilter("EQS_state","1");
		filters.add(p);
		academyList=academyManager.getListByFilter(filters);
		entity=academyList.get(0);
		
		return "list";
	}

	// 异步查询访问人数
	public String visitorCount() throws IOException {
		String path = ServletActionContext.getServletContext().getRealPath(
				"/WEB-INF");
		String r = "";
		path += "/other/visitCount.properties";
		File f = new File(path);
		if (f.exists()) {
			f.createNewFile();
		}
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		if (session.get(id) == null) {
			r = FileUtils.getProperties(path, id, "1", true);
			session.put(id, "1");
		} else {
			r = FileUtils.getProperties(path, id, "1", false);
		}

		Struts2Utils.renderText(r);
		return null;
	}
	@Override
	public String input() throws Exception {
		entity=academyManager.getAcademyById(id);
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p=new PropertyFilter("EQS_state","1");
		filters.add(p);
		academyList=academyManager.getListByFilter(filters);	
		return "detail";
	}

	@Override
	public String save() throws Exception {
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(StringUtils.isEmpty(id)){
			entity = new Academy();
		}else{
			entity = academyManager.getAcademyById(id);
		}
		
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	public Academy getEntity() {
		return entity;
	}

	public void setEntity(Academy entity) {
		this.entity = entity;
	}
    @Autowired
	public void setAcademyManager(AcademyManager academyManager) {
		this.academyManager = academyManager;
	}

	public List<Academy> getAcademyList() {
		return academyList;
	}

	public void setAcademyList(List<Academy> academyList) {
		this.academyList = academyList;
	}

	public AcademyManager getAcademyManager() {
		return academyManager;
	}

	public MajorManager getMajorManager() {
		return majorManager;
	}
    @Autowired
	public void setMajorManager(MajorManager majorManager) {
		this.majorManager = majorManager;
	}

	public List<Major> getMajorList() {
		return majorList;
	}

	public void setMajorList(List<Major> majorList) {
		this.majorList = majorList;
	}
}
